package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.ReLangDict;
import com.iretailer.report.mapper.LangDictMapper;
import com.iretailer.report.model.LangDict;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


@Service
public class LangDictService extends ServiceImpl<LangDictMapper, LangDict> {

    private final LangDictMapper langDictMapper;

    public LangDictService(LangDictMapper langDictMapper) {
        this.langDictMapper = langDictMapper;
    }

    public PageResult<LangDict> langDictPage(ReLangDict reLangDict) {
        QueryOption queryOption = reLangDict.getQuery();
        QueryWrapper<LangDict> queryWrapper = new QueryWrapper<>();

        LambdaQueryWrapper<LangDict> lambdaWrapper = queryWrapper.lambda();
        String chineseValue = reLangDict.getChineseValue();
        if (StringUtils.isNotBlank(chineseValue)) {
            lambdaWrapper.like(LangDict::getChineseValue, chineseValue);
        }
        String englishValue = reLangDict.getEnglishValue();
        if (StringUtils.isNotBlank(englishValue)) {
            lambdaWrapper.like(LangDict::getEnglishValue, englishValue);
        }
        String japaneseValue = reLangDict.getJapaneseValue();
        if (StringUtils.isNotBlank(japaneseValue)) {
            lambdaWrapper.like(LangDict::getJapaneseValue, japaneseValue);
        }
        String langDictKey = reLangDict.getLangDictKey();
        if (StringUtils.isNotBlank(langDictKey)) {
            lambdaWrapper.like(LangDict::getLangDictKey, langDictKey);
        }
        Page<LangDict> page = new Page<>();
        queryOption.judge(page,queryWrapper);
        IPage<LangDict> resultPage = langDictMapper.selectPage(page, queryWrapper);
        return  new PageResult<>(resultPage.getTotal(), resultPage.getRecords());
    }
}
